﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinook.Facade.Messaging;
using Chinook.Core.DaoInterface;
using Chinook.Facade.Ioc;
using Chinook.Data;
using Chinook.Facade.ViewModel;
using Chinook.Core.DomainModel;
using System.Linq.Expressions;
using System.Linq;



namespace Chinook.Facade
{
   public class AlbumAppService : IAlbumAppService
    {
        private IAlbumDao _albumDao;

        public IAlbumDao AlbumDao
        {
            get
            {
                if (_albumDao != null)
                    return _albumDao;
                _albumDao = new FacadeExposer().AlbumDao;
                return _albumDao;
            }
        }


        #region IAlbumAppService Members

        public AlbumSearchResponse SearchAlbum(AlbumSearchRequest albumSearchRequest)
        {
            AlbumSearchResponse result = new AlbumSearchResponse();
            try
            {
                var albums = AlbumDao.GetPaged(albumSearchRequest.PageIndex,albumSearchRequest.PageSize);

                List<AlbumViewModel> albumViewModel = new List<AlbumViewModel>();
                foreach (Album a in albums)
                {                  
                    albumViewModel.Add(new AlbumViewModel() { Title = a.Title,Id=a.Id });
                }
                result.Success = true;
                result.PageSize = albums.PageSize;
                result.PageIndex = albums.PageIndex;
                result.TotalCount = albums.TotalCount;
                result.AlbumViewModel = albumViewModel;

                //Expression<Func<Album, bool>> predicate = x => x.Title.StartsWith("Black");
                Expression<Func<Album, bool>> predicate = pr => pr.Title.StartsWith("%Black%");
                
                    
                //ICriterion equalCriterion = Restrictions.Where<Cat>(c => c.Name == "Max")

                var results = AlbumDao.GetSingle(predicate);

            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;           
        }

        #endregion
    }
}
